﻿using System;
using System.Data;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxTreeList;
using NguyenHoa.Services;
using NguyenHoa.Web.App_Code;

namespace NguyenHoa.Web.Pages
{
  public partial class Sitemap : BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      CreateNodes();
    }

    private void CreateNodes()
    {
      DataSet dataSet = new MainMenuService().GetMainMenuByLanguage(CurrentLanguage.Id, MenuTypes.FrontEnd, null);
      if (dataSet != null && dataSet.Tables[0].Rows.Count > 0)
      {
        int count = 0;
        TreeListNode treeListNode;
        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
        {
          int id = Convert.ToInt32(dataRow["Id"]);
          string title = dataRow["Title"].ToString();
          string link = dataRow["Link"].ToString();
          treeListNode = CreateNodeCore(count++, "<b>" + title + "</b>", link, null);
          treeListNode.Expanded = true;
          DataSet dsSubmenu = new SubMenuService().GetSubMenuByLanguage(id, CurrentLanguage.Id, null);
          if (dsSubmenu != null && dsSubmenu.Tables[0].Rows.Count > 0)
          {
            foreach (DataRow drSub in dsSubmenu.Tables[0].Rows)
            {
              string sTitle = drSub["Title"].ToString();
              string sLink = drSub["Link"].ToString();
              CreateNodeCore(count++, sTitle, sLink, treeListNode);
            }
          }
        }
      }
    }

    TreeListNode CreateNodeCore(object key, string title, string link, TreeListNode parentNode)
    {
      TreeListNode node = tlSiteMap.AppendNode(key, parentNode);
      node["Title"] = title;
      node["Link"] = link;
      return node;
    }

    protected string GetUrl(TreeListDataCellTemplateContainer container)
    {
      return container.GetValue("Link").ToString();
    }

  }
}
